/*
 * @Descripttion:
 * @version:
 * @Author: ada.wu
 * @Date: 2023-12-04 23:15:32
 * @LastEditors: ada.wu
 * @LastEditTime: 2023-12-18 14:58:04
 */
import { defineStore } from "pinia";
import { ref } from "vue";

export const useWebSocketStore = defineStore("socket", {
    // 其他配置...
    state: () => ({
        onLine: navigator.onLine,
        // 这里是你的状态
        socket: null,
        checkTask: null,
        readyState: 0,
        socketMessage: "",
    }),
    getters: {

    },
    actions: {
        //  actions
        connectWebSocket() {
            this.onLine = true
            let _this = this
            // console.log('connectWebSocket====')
            const PING_INTERVAL = 3000; // 心跳间隔，单位为毫秒
            var url = window.location.href;
            var urlReg = /[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62}|(:[0-9]{1,4}))+\.?/;
            var subUrl = urlReg.exec(url);
            // console.log(subUrl[0])
            //正式
            const HOST_ADDRESS = "ws://" + subUrl[0] + "/remote/ws"
            //测试
            // http://139.9.193.230:4100/api/voip/handle?event=connect&room=E302&user=2022
            // const HOST_ADDRESS = "ws://139.9.193.230:4100/remote/ws"
            // console.log('HOST_ADDRESS----', HOST_ADDRESS)
            this.socket = ref(new WebSocket(HOST_ADDRESS))
            // console.log('socket===', socket)
            this.checkTask = null
            // 监听连接事件
            this.socket.onopen = (event) => {
                // 启动心跳检测 确保连接存活 客户端每隔5秒向服务端发送一次心跳消息
                // console.log('连接====', heartbeatMessage, this.onLine)
                this.checkTask && clearInterval(this.checkTask)
                this.checkTask = setInterval(() => {
                    this.socket.send('')
                }, PING_INTERVAL)
            }
            // 监听消息事件
            this.socket.onmessage = (event) => {
                // console.log('消息====', event)
                _this.socketMessage = ''
                if (event.data && event.data != 'ok') {
                    _this.socketMessage = event.data
                }

            }
            this.oncloseWebSocket()
            // 连接错误
            this.socket.onerror = (event) => {
                // console.log('连接错误====', event)
            }
        },

        oncloseWebSocket() {
            // 监听关闭事件 断线重连
            this.socket.onclose = (event) => {
                // console.log('断线重连====', event, this.onLine)
                if (this.onLine) {
                    // 清除心跳计时器
                    this.checkTask && clearInterval(this.checkTask)
                    // 断线重连
                    setTimeout(() => {
                        this.connectWebSocket()
                    }, 3000)
                }
            }
        },
        closeWebSocket() {
            this.onLine = false
            if (this.socket && this.socket.readyState === WebSocket.OPEN) {
                this.socket.close();
                clearInterval(this.checkTask)
            }
        },

    },
});
